//
//  JoinQuizDetailCell.m
//  WanCaiC8
//
//  Created by tangang on 2018/3/14.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "JoinQuizDetailCell.h"
#import "LabCell.h"
#import "LotteryNumCell.h"
@interface JoinQuizDetailCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic ,strong) UICollectionView *collectionView;
@end

@implementation JoinQuizDetailCell{
    JoinQuizModel   *joinQuizModel;
    QXLable         *labTitle;
    UIView          *endLine;
    NSMutableArray  *dataArr;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        WS(weakSelf);
        labTitle=[[QXLable alloc]initWithFount:13];
        labTitle.textAlignment=NSTextAlignmentCenter;
        [self.contentView addSubview:labTitle];
        [labTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.equalTo(weakSelf.contentView);
            make.width.mas_equalTo(@70);
        }];
        
        [self.contentView addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(weakSelf.contentView);
            make.left.equalTo(labTitle.mas_right);
            make.width.mas_equalTo(@(SCREEN_WIDTH-150));
        }];
        
        UIButton *btnSelect=[[UIButton alloc]init];
        [btnSelect setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btnSelect setTitle:@"选择号码" forState:UIControlStateNormal];
        [btnSelect.titleLabel setFont:[UIFont systemFontOfSize:13]];
        [btnSelect addTarget:self action:@selector(onclickSelect) forControlEvents:UIControlEventTouchUpInside];
        btnSelect.backgroundColor=NAVCOLOR;
        [self.contentView addSubview:btnSelect];
        [btnSelect mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.bottom.equalTo(weakSelf.contentView);
            make.left.equalTo(weakSelf.collectionView.mas_right);
        }];
        
        UIView *line=[self lineView];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(weakSelf.contentView);
            make.height.mas_equalTo(@.7);
        }];
        
        UIView *line1=[self lineView];
        [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(weakSelf.contentView);
            make.left.equalTo(labTitle.mas_right);
            make.width.mas_equalTo(@.5);
        }];
        
        endLine=[self lineView];
        endLine.hidden=YES;
        [endLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(weakSelf.contentView);
            make.height.mas_equalTo(@.7);
        }];
        
    }
    return self;
}

- (UIView *)lineView{
    UIView *line=[[UIView alloc]init];
    line.backgroundColor=DEFAULTTEXTGRAYCOLOR;
    [self.contentView addSubview:line];
    return line;
}

- (void)setModel:(JoinQuizModel *)model andIsLast:(BOOL)isLast{
    joinQuizModel=model;
    labTitle.text=model.title;
    endLine.hidden=!isLast;
    dataArr=[joinQuizModel.dataArr mutableCopy];
    [dataArr removeObject:@""];
    [self.collectionView reloadData];
}

- (void)onclickSelect{
    if (self.selectNumBlock) {
        self.selectNumBlock();
    }
}

#pragma mark - collectionView代理函数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.lType==LotteryNumTypeLottery&&joinQuizModel.dataArr.count>0) {
        if ([joinQuizModel.title isEqualToString:@"平五码"]||[joinQuizModel.title isEqualToString:@"二十码中特"]||[joinQuizModel.title isEqualToString:@"十码中特"]||[joinQuizModel.title isEqualToString:@"五不中"]||[joinQuizModel.title isEqualToString:@"六不中"]) {
            NSString *str=joinQuizModel.dataArr[0];
            if (str.length>1) {
                NSArray *arr=[str componentsSeparatedByString:@","];
                return arr.count;
            }
        }
        
        
        
        
    }
    return dataArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.lType==LotteryNumTypeLottery&&joinQuizModel.dataArr.count>0) {
        
        if ([joinQuizModel.title isEqualToString:@"平五码"]||[joinQuizModel.title isEqualToString:@"二十码中特"]||[joinQuizModel.title isEqualToString:@"十码中特"]||[joinQuizModel.title isEqualToString:@"五不中"]||[joinQuizModel.title isEqualToString:@"六不中"]) {
            NSString *str=joinQuizModel.dataArr[0];
            if (str.length>1) {
                NSArray *arr=[str componentsSeparatedByString:@","];
                LotteryNumCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"LotteryNumCell" forIndexPath:indexPath];
                [cell setTitle:arr[indexPath.row] andType:LotteryNumTypeLottery];
                return cell;
            }
        }
        
        
    }
    LabCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"LabCell" forIndexPath:indexPath];
    [cell setRedTitle:dataArr[indexPath.row]];
    cell.backgroundColor=[UIColor whiteColor];
    if (dataArr.count==3){
        if (indexPath.row>0) {
            cell.backgroundColor=DEFAULTBGCOLOR;
        }
    }else if (dataArr.count==4){
        if (indexPath.row>1) {
            cell.backgroundColor=DEFAULTBGCOLOR;
        }
    }else if (dataArr.count==7){
        if (indexPath.row==1||indexPath.row==2||indexPath.row>4) {
            cell.backgroundColor=DEFAULTBGCOLOR;
        }
    }
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.lType==LotteryNumTypeLottery&&joinQuizModel.dataArr.count>0) {
        if ([joinQuizModel.title isEqualToString:@"平五码"]||[joinQuizModel.title isEqualToString:@"二十码中特"]||[joinQuizModel.title isEqualToString:@"十码中特"]||[joinQuizModel.title isEqualToString:@"五不中"]||[joinQuizModel.title isEqualToString:@"六不中"]) {
            NSString *str=joinQuizModel.dataArr[0];
            if (str.length>1) {
                return CGSizeMake(real_size(26), real_size(26));
            }
        }
    }
    if (dataArr.count%2==1) {
        if (indexPath.row==0) {
            return CGSizeMake((SCREEN_WIDTH-150), 50);
        }
    }
    return CGSizeMake((SCREEN_WIDTH-150)/2.0, 50);
}


- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout =
        [[UICollectionViewFlowLayout alloc] init];
        [flowLayout setItemSize:CGSizeMake((SCREEN_WIDTH-150)/2, 50)];
        flowLayout.minimumLineSpacing=0;
        flowLayout.minimumInteritemSpacing=0;
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero
                                             collectionViewLayout:flowLayout];
        [_collectionView registerClass:[LabCell class] forCellWithReuseIdentifier:@"LabCell"];
        [_collectionView registerClass:[LotteryNumCell class] forCellWithReuseIdentifier:@"LotteryNumCell"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
    }
    return _collectionView;
}

@end
